#define _CRT_SECURE_NO_WARNINGS

#include "Sort.h"

void Swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void CountSort(int* a, int n)
{
	int max = a[0];
	int min = a[0];

	for (int i = 1; i < n; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
		if (a[i] < min)
		{
			min = a[i];
		}
	}

	int range = max - min + 1;

	int* tmp = (int*)calloc(range, sizeof(int));
	if (tmp == NULL)
	{
		perror("calloc fail");
		return;
	}

	for (int i = 0; i < n; i++)
	{
		tmp[a[i]-min]++;
	}

	int j = 0;
	for (int i = 0; i < range; i++)
	{
		while (tmp[i]--)
		{
			a[j++] = i+min;
		}
	}

	free(tmp);
	tmp = NULL;

}

void InsertSort(int* a, int n)
{

	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = a[end + 1];

		while (end >= 0)
		{
			if (a[end] > tmp)
			{
				a[end + 1] = a[end];
				--end;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = tmp;
	}
	
}

void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = a[end + gap];

			while (end >= 0)
			{
				if (a[end] > tmp)
				{
					a[end + gap] = a[end];
					end-=gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tmp;
		}
	}
	

}

void SelectSort(int* a, int n)
{
	int begin = 0;
	int end = n-1;

	while (begin < end)
	{
		int maxi = begin;
		int mini = begin;

		for (int i = begin+1; i <= end; i++)
		{
			if (a[i] > a[maxi])
			{
				maxi = i;
			}

			if (a[i] < a[mini])
			{
				mini = i;
			}
		}

		if (maxi == begin && mini == end)
		{
			Swap(&a[begin], &a[mini]);
		}

		if (maxi == begin)
		{
			Swap(&a[maxi], &a[end]);
			Swap(&a[begin], &a[mini]);
		}

		if (mini == end)
		{
			Swap(&a[begin], &a[mini]);
			Swap(&a[maxi], &a[end]);
		}
		else
		{
			Swap(&a[begin], &a[mini]);
			Swap(&a[maxi], &a[end]);
		}
		++begin;
		--end;
	}
	


}

void AdjustDown(int* a, int n,int parent)
{
	int child = parent * 2 + 1;

	while (child < n)
	{
		if (child + 1 < n && a[child] < a[child + 1])
		{
			++child;
		}

		if (a[parent] < a[child])
		{
			Swap(&a[parent], &a[child]);
			parent = child;
			child = parent * 2 + 1;
		}

		else
		{
			break;
		}
	}
}

void HeapSort(int* a, int n)
{
	for (int i = n - 1; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}

	int end = n - 1;

	while (end > 0)
	{
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
} 

int GetMidi(int* a, int left,int right)
{
	int midi = (left + right) / 2;

	if (a[left] < a[midi])
	{
		if (a[midi] < a[right])
		{
			return midi;
		}

		if (a[left] < a[right])
		{
			return right;
		}

		else
		{
			return left;
		}
	}
	else
	{
		if (a[midi] > a[right])
		{
			return midi;
		}

		if (a[left] > a[right])
		{
			return right;
		}

		else
		{
			return left;
		}
	}


}

int PartSort01(int* a, int left, int right)
{
	int midi = GetMidi(a, left, right);
	Swap(&a[left], &a[midi]);

	int begin = left;
	int end = right;
	int tmpi = left;

	while (begin < end)
	{
		while (begin<end&&a[end] >= a[tmpi])
		{
			--end;
		}
		while (begin < end && a[begin] <= a[tmpi])
		{
			++begin;
		}
		Swap(&a[begin], &a[end]);
	}
	Swap(&a[begin], &a[tmpi]);
	tmpi = begin;

	return tmpi;
}

int PartSort02(int* a, int left, int right)
{
	int midi = GetMidi(a, left, right);
	Swap(&a[left], &a[midi]);

	int begin = left;
	int end = right;
	int tmpi = left;
	int tmp = a[tmpi];

	while (begin<end)
	{
		while (begin < end && a[end] >= tmp)
		{
			--end;
		}
		a[tmpi] = a[end];
		tmpi = end;

		while (begin < end && a[begin] <= tmp)
		{
			++begin;
		}
		a[tmpi] = a[begin];
		tmpi = begin;

	}

	a[tmpi] = tmp;
	return tmpi;
}

int PartSort03(int* a, int left, int right)
{
	int midi = GetMidi(a, left, right);
	Swap(&a[left], &a[midi]);

	int prev = left;
	int cur = prev + 1;
	int keyi = left;

	while (cur<=right)
	{
		if (a[cur] < a[keyi])
		{
			++prev;
			if (prev != cur)
			{
				Swap(&a[prev], &a[cur]);
			}
		}
	}
	Swap(&a[keyi], &a[prev]);
	return keyi;
}

void QuickSort(int* a, int left, int right)
{
	if (left >= right)
	{
		return;
	}

	if ((right - left + 1) < 10)
	{
		InsertSort(a, right - left + 1);
	}

	else
	{
		int keyi = PartSort01(a, left, right);

		QuickSort(a, left, keyi - 1);
		QuickSort(a, keyi + 1, right);
	}
	
}



